﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Gw2ApiTestApp
{

    class GenerateEBXaml
    {
        public static string template = @"        <Path Stroke=""{{StaticResource Brush-Outline}}"" StrokeThickness=""1"" Fill=""LawnGreen"" Tag=""{4}"">
            <Path.Data>
                <PathGeometry>
                    <PathGeometry.Figures>
                        <PathFigureCollection>
                            <PathFigure StartPoint=""0,0"">
                                <PathFigure.Segments>
                                    <PathSegmentCollection>
                                        <LineSegment>
                                            <LineSegment.Point>
                                                <MultiBinding Converter=""{{StaticResource PercentageConverter}}"">
                                                    <MultiBinding.ConverterParameter>
                                                        <x:Array Type=""mscor:Double"">
                                                            <StaticResource ResourceKey=""{0}"" />
                                                            <StaticResource ResourceKey=""{2}"" />
                                                        </x:Array>
                                                    </MultiBinding.ConverterParameter>
                                                    <Binding ElementName=""grid"" Path=""ActualHeight""/>
                                                    <Binding ElementName=""grid"" Path=""ActualWidth""/>
                                                </MultiBinding>
                                            </LineSegment.Point>
                                        </LineSegment>
                                        <ArcSegment IsLargeArc=""False"" SweepDirection=""Clockwise"" >
                                            <ArcSegment.Point>
                                                <MultiBinding Converter=""{{StaticResource PercentageConverter}}"">
                                                    <MultiBinding.ConverterParameter>
                                                        <x:Array Type=""mscor:Double"">
                                                            <StaticResource ResourceKey=""{0}"" />
                                                            <StaticResource ResourceKey=""{1}"" />
                                                        </x:Array>
                                                    </MultiBinding.ConverterParameter>
                                                    <Binding ElementName=""grid"" Path=""ActualHeight""/>
                                                    <Binding ElementName=""grid"" Path=""ActualWidth""/>
                                                </MultiBinding>
                                            </ArcSegment.Point>
                                            <ArcSegment.Size>
                                                <MultiBinding Converter=""{{StaticResource PercentageConverter}}"">
                                                    <MultiBinding.ConverterParameter>
                                                        <x:Array Type=""mscor:Double"">
                                                            <mscor:Double>0.03</mscor:Double>
                                                            <mscor:Double>0.1</mscor:Double>
                                                        </x:Array>
                                                    </MultiBinding.ConverterParameter>
                                                    <Binding ElementName=""grid"" Path=""ActualHeight""/>
                                                    <Binding ElementName=""grid"" Path=""ActualWidth""/>
                                                </MultiBinding>
                                            </ArcSegment.Size>
                                        </ArcSegment>
                                        <LineSegment Point=""0,0""/>
                                    </PathSegmentCollection>
                                </PathFigure.Segments>
                            </PathFigure>
                        </PathFigureCollection>
                    </PathGeometry.Figures>
                </PathGeometry>
            </Path.Data>
            <Path.RenderTransform>
                <TransformGroup>
                    <RotateTransform CenterX=""0"" CenterY=""0"" Angle=""{{StaticResource {3}}}"" />
                    <TranslateTransform>
                        <TranslateTransform.X>
                            <MultiBinding Converter=""{{StaticResource PercentageConverter}}"" ConverterParameter=""0.5"">
                                <Binding ElementName=""grid"" Path=""ActualWidth""/>
                            </MultiBinding>
                        </TranslateTransform.X>
                        <TranslateTransform.Y>
                            <MultiBinding Converter=""{{StaticResource PercentageConverter}}"" ConverterParameter=""0.5"">
                                <Binding ElementName=""grid"" Path=""ActualHeight""/>
                            </MultiBinding>
                        </TranslateTransform.Y>
                    </TranslateTransform>
                </TransformGroup>
            </Path.RenderTransform>
        </Path>";

        public static string defTemplate = @"<mscor:Double x:Key=""{0}"">{1}</mscor:Double>";

        public static string ebTemplateOpen = @"<Grid x:Name=""EB-Structures"" Visibility=""Collapsed"">";
        public static string ebTemplateClose = @"</Grid>";

        public static string blTemplateOpen = @"<Grid x:Name=""BL-Structures"" Visibility=""Visible"">";
        public static string blTemplateClose = @"</Grid>";

        static void Main(string[] args)
        {
            double[] keepAngles = { -90, 30, 150 };
            double[] towerDeviation = { 25, 48 };
            double[] campDeviation = { 37 };
            double[] blkeepAngles = { 0, 180 };
            double[] bltowerDeviation = { 50 };
            double[] blcampDeviation = { 30 };
            double blruinAngle = 360.0 / 5;
            Dictionary<string,double> structures = new Dictionary<string,double>(){ 
                                {"EB-R-Keep", keepAngles[0]},
                                {"EB-B-Keep", keepAngles[1]},
                                {"EB-G-Keep", keepAngles[2]},
                                
                                {"EB-R-1-Tower", keepAngles[0] - towerDeviation[1]},
                                {"EB-R-2-Tower", keepAngles[0] - towerDeviation[0]},
                                {"EB-R-3-Tower", keepAngles[0] + towerDeviation[0]},
                                {"EB-R-4-Tower", keepAngles[0] + towerDeviation[1]},
                                                        
                                {"EB-B-1-Tower", keepAngles[1] - towerDeviation[1]},
                                {"EB-B-2-Tower", keepAngles[1] - towerDeviation[0]},
                                {"EB-B-3-Tower", keepAngles[1] + towerDeviation[0]},
                                {"EB-B-4-Tower", keepAngles[1] + towerDeviation[1]},

                                {"EB-G-1-Tower", keepAngles[2] - towerDeviation[1]},
                                {"EB-G-2-Tower", keepAngles[2] - towerDeviation[0]},
                                {"EB-G-3-Tower", keepAngles[2] + towerDeviation[0]},
                                {"EB-G-4-Tower", keepAngles[2] + towerDeviation[1]},

                                {"EB-R-1-Camp", keepAngles[0] - campDeviation[0]},
                                {"EB-R-2-Camp", keepAngles[0] + campDeviation[0]},
                                            
                                {"EB-G-1-Camp", keepAngles[1] - campDeviation[0]},
                                {"EB-G-2-Camp", keepAngles[1] + campDeviation[0]},
                                            
                                {"EB-B-1-Camp", keepAngles[2] - campDeviation[0]},
                                {"EB-B-2-Camp", keepAngles[2] + campDeviation[0]},
                               };
            Dictionary<string, double> blstructures = new Dictionary<string, double>(){ 
                                {"BL-E-Keep", blkeepAngles[0]},
                                {"BL-W-Keep", blkeepAngles[1]},
                                                                
                                {"BL-NE-Tower", blkeepAngles[0] - bltowerDeviation[0]},
                                {"BL-SE-Tower", blkeepAngles[0] + bltowerDeviation[0]},
                                {"BL-SW-Tower", blkeepAngles[1] - bltowerDeviation[0]},
                                {"BL-NW-Tower", blkeepAngles[1] + bltowerDeviation[0]},

                                {"BL-N-Camp", -90},
                                {"BL-NE-Camp", blkeepAngles[0] - blcampDeviation[0]},
                                {"BL-SE-Camp", blkeepAngles[0] + blcampDeviation[0]},
                                {"BL-S-Camp", 90},
                                {"BL-SW-Camp", blkeepAngles[1] - blcampDeviation[0]},
                                {"BL-NW-Camp", blkeepAngles[1] + blcampDeviation[0]},

                                {"BL-S-Ruin", 90},
                                {"BL-SW-Ruin", 90 + blruinAngle},
                                {"BL-NW-Ruin", 90 + blruinAngle * 2},
                                {"BL-NE-Ruin", 90 + blruinAngle * 3},
                                {"BL-SE-Ruin", 90 + blruinAngle * 4},
                               };
            string[] lengths = structures.Select(kv => kv.Key + "-Length").ToArray();
            string[] widths = structures.Select(kv => kv.Key + "-Width").ToArray();
            string[] widthnegs = structures.Select(kv => kv.Key + "-Width-Neg").ToArray();
            string[] angles = structures.Select(kv => kv.Key + "-Angle").ToArray();
            string[] namesOnly = structures.Select(kv => kv.Key).ToArray();
            Dictionary<string, double> lengthConsts = new Dictionary<string, double>(){
                {"Keep", 0.28},
                {"Tower", 0.37},
                {"Camp", 0.48},
            };
            Dictionary<string, double> widthConsts = new Dictionary<string, double>(){
                {"Keep", 0.1},
                {"Tower", 0.06},
                {"Camp", 0.03},
            };
            writeFiles("curvedefs.txt", "constdefs.xaml", lengths, widths, widthnegs, angles, namesOnly, lengthConsts, widthConsts, structures, ebTemplateOpen, ebTemplateClose);

            lengths = blstructures.Select(kv => kv.Key + "-Length").ToArray();
            widths = blstructures.Select(kv => kv.Key + "-Width").ToArray();
            widthnegs = blstructures.Select(kv => kv.Key + "-Width-Neg").ToArray();
            angles = blstructures.Select(kv => kv.Key + "-Angle").ToArray();
            namesOnly = blstructures.Select(kv => kv.Key).ToArray();
            lengthConsts = new Dictionary<string, double>(){
                {"Keep", 0.28},
                {"Tower", 0.37},
                {"Camp", 0.48},
                {"Ruin", 0.155},
            };
            widthConsts = new Dictionary<string, double>(){
                {"Keep", 0.14},
                {"Tower", 0.08},
                {"Camp", 0.045},
                {"Ruin", 0.10},
            };
            writeFiles("blcurvedefs.txt", "blconstdefs.xaml", lengths, widths, widthnegs, angles, namesOnly, lengthConsts, widthConsts, blstructures, blTemplateOpen, blTemplateClose);
        }

        public static void writeFiles(string curvefilename, string constfilename, string[] lengths, string[] widths, string[] widthnegs,
                                        string[] angles, string[] namesOnly, Dictionary<string, double> lengthConsts,
                                        Dictionary<string, double> widthConsts, Dictionary<string, double> structures,
                                        string open, string close)
        {
            using (System.IO.StreamWriter file = new System.IO.StreamWriter(curvefilename))
            {
                file.WriteLine(open);
                for (int i = 0; i < structures.Count; i++)
                {
                    file.WriteLine(template, lengths[i], widths[i], widthnegs[i], angles[i], namesOnly[i]);
                }
                file.WriteLine(close);
            }

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(constfilename))
            {
                file.WriteLine(@"<ResourceDictionary  xmlns=""http://schemas.microsoft.com/winfx/2006/xaml/presentation""
                     xmlns:x=""http://schemas.microsoft.com/winfx/2006/xaml""
                     xmlns:mscor=""clr-namespace:System;assembly=mscorlib"">");
                for (int i = 0; i < lengths.Length; i++)
                {
                    foreach (var kv in lengthConsts.Where(kv => lengths[i].Contains(kv.Key)))
                    {
                        file.WriteLine(defTemplate, lengths[i], kv.Value);
                    }
                    foreach (var kv in widthConsts.Where(kv => widths[i].Contains(kv.Key)))
                    {
                        file.WriteLine(defTemplate, widths[i], kv.Value);
                    }
                    foreach (var kv in widthConsts.Where(kv => widthnegs[i].Contains(kv.Key)))
                    {
                        file.WriteLine(defTemplate, widthnegs[i], -kv.Value);
                    }
                }
                foreach (var kv in structures)
                {
                    file.WriteLine(defTemplate, angles.Where(v => v.StartsWith(kv.Key)).First(), kv.Value);
                }
                file.WriteLine(@"</ResourceDictionary>");
            }
        }
    }
}
